import Base from './base.js';
import {getNonce} from '../../common/utils/utils'
export default class extends Base {
      
      $verify_login = false;

      async indexAction() {
            const user = await this.model('user')
            const query = {'user_name':this.post('username')};
            const userInfo = await user.field('password,salt,user_name').where(query).find()

            if(think.isEmpty(userInfo)){
                  return this.fail(1004,'用户不存在');
            }

            const userPwd = think.md5(this.post('password')+this.post('sign')+userInfo.salt)
            if(userPwd!==userInfo.password){
                  return this.fail(1004,'密码错误');
            }

            const token = think.md5(getNonce(20));

            await user.where(query).update({last_login_time: new Date().getTime()/1000,last_login_ip:this.ip,token});

            return this['_success']({token},'成功')
      }
}
